import { environment } from '../../../environments/environment';
import { EventEmitter, Injectable } from '@angular/core';
import { LangChangeEvent, TranslateService } from '@ngx-translate/core';
import { Observable } from 'rxjs';
import { CoreModule } from './../core.module';
import { StorageService } from '../sotrage/storage.service';



@Injectable({
  providedIn: CoreModule
})
export class I18nService {

  constructor(
    private translate: TranslateService,
    private sto: StorageService,
  ) {
    // 添加语言支持
    translate.addLangs(['en', 'zh', 'jp']);
    // 设置默认语言，一般在无法匹配的时候使用
    let l = 'zh';
    this.sto.get('lang') ? l = this.sto.get('lang') : 'zh';
    // let a = this.sto.get('lang')
    translate.setDefaultLang(l);
    // 获取当前浏览器环境的语言比如en、 zh
    const browserLang = translate.defaultLang;
    translate.use(browserLang.match(/en|zh|jp/) ? browserLang : 'zh');

  }

  // 切换语言
  changeLang(lang) {
    this.translate.use(lang);
    this.sto.set('lang',lang);
  }

  // 翻译
  get(key: string | Array<string>, interpolateParams?: Object): Observable<string | any> {
    return this.translate.get(key, interpolateParams);
  }

  onLangChange(): EventEmitter<LangChangeEvent> {
    return this.translate.onLangChange;
  }

  // getRegionNumbers(): ObservableResponse<RegionNumber[]> {
  //   return this.api.post<CommonResponse<RegionNumber[]>>(urlService(environment.type, '获取国家区号列表(未登入)'));
  // }
}
